angular.module('nonghua.controllers.node', [])

    .controller('CaseListCtrl', function ($scope,  $stateParams, $ionicLoading, $rootScope) {
        articleListCtrl($scope, $ionicLoading, $rootScope, "case", "caseList");
    })

    .controller('NewsListCtrl', function ($scope,  $stateParams, $ionicLoading, $rootScope) {
        articleListCtrl($scope, $ionicLoading, $rootScope, "news", "newsList");
    })

    .controller('KnowledgeListCtrl', function ($scope,  $stateParams, $ionicLoading, $rootScope) {
        articleListCtrl($scope, $ionicLoading, $rootScope, "knowledge", "knowledgeList");
    })

    .controller('MarketListCtrl', function ($scope,  $stateParams, $ionicLoading, $rootScope) {
        articleListCtrl($scope, $ionicLoading, $rootScope, "market", "marketList");
    })

    .controller('ArticleDetailCtrl', function ($scope,$ionicLoading, $stateParams, $rootScope, $ionicHistory) {
        $scope.nid = $stateParams.nid;
        $scope.lid = $stateParams.lid;
        $scope.node = $rootScope[$scope.lid][$scope.nid];
    })

    .controller('ConsultListCtrl', function ($scope, $ionicSideMenuDelegate, $ionicLoading, $rootScope, $state, $stateParams) {
        $scope.viewMode = $stateParams.viewMode;
        $scope.switchMode = function (mode) {
            if ($scope.viewMode != mode) {
                $scope.viewMode = mode;
                $scope.reloadList();
            }
        };

        $scope.addConsult = function () {
            $state.go('tabs.consult-add');
        };

        $scope.initlist = function () {
            $rootScope.consultList = {};
            $scope.listEnd = false;
            $scope.latest = "";
            $scope.oldest = "";
        };

        $scope.reloadList = function () {
            $scope.initlist();
            $scope.loadList();
        };

        $scope.loadList = function (refresh) {
            var path_to_view;
            if ($scope.viewMode == 0) {
                path_to_view = 'app-consult-list/all';
            } else {
                path_to_view = 'app-consult-list/' + $rootScope.user.uid;
            }

            if(refresh) {
                //getting latest comments
                if ($scope.latest != '') {
                    path_to_view = path_to_view + '/' + $scope.latest + "--" + $rootScope.newlimit;
                }
            } else {
                //loading old comments
                if ($scope.oldest != '') {
                    path_to_view = path_to_view + '/' + $rootScope.oldlimit + "--" + $scope.oldest;
                }
            }

            console.log(path_to_view);
            views_datasource_get_view_result(path_to_view, {
                success: function (data) {
                    var length = data.nodes.length;
                    if (length != 0) {
                        console.log(data);
                        for (var i=0; i<length; i++) {
                            var nid = data.nodes[i].node.nid;
                            $rootScope['consultList'][nid] = {};
                            $rootScope['consultList'][nid].nid = nid;
                            $rootScope['consultList'][nid].title = data.nodes[i].node.title;
                            //$rootScope['consultList'][nid].body = data.nodes[i].node.body;
                            if (data.nodes[i].node.author_pic) {
                                $rootScope['consultList'][nid].author_pic = data.nodes[i].node.author_pic;
                            } else {
                                //to-do add consult default pic
                            }
                            $rootScope['consultList'][nid].time = data.nodes[i].node.time;
                        }
                        console.log($rootScope['consultList']);
                        if (!refresh) {
                            //如果上拉加载需要将时间戳下线设为本次数据最旧的时间
                            $scope.oldest = data.nodes[length - 1].node.nid;
                        }
                        if (refresh && data.nodes[length - 1].node.nid > $scope.latest) {
                            //如果下拉刷新且最旧的数据的时间戳都比目前上限要新
                            $scope.oldest = data.nodes[length - 1].node.nid;
                            $scope.listEnd = false;
                        }
                        if (refresh || $scope.latest == '') {
                            //如果下拉刷新需要重置最新时间
                            $scope.latest = data.nodes[0].node.nid;
                        }
                    }
                    if (!refresh && length < 10) {
                        $scope.listEnd = true;
                    }
                    $scope.$apply();
                    if (refresh) {
                        $scope.$broadcast('scroll.refreshComplete');
                    } else {
                        $scope.$broadcast('scroll.infiniteScrollComplete');
                    }
                }
            });
        };

        $scope.initlist();
    })

    .controller('ConsultDetailCtrl', function ($scope, $stateParams, $rootScope) {
        $scope.nid = $stateParams.nid;
        $scope.lid = $stateParams.lid;

        $scope.consult = $rootScope[$scope.lid][$scope.nid];

        $scope.latest = "";
        $scope.oldest = "";
        $scope.comment = {};

        $scope.loadList = function (refresh) {
            var path_to_view = 'app-consult-answers/' + $scope.nid;

            if(refresh) {
                //getting latest comments
                if ($scope.latest != '') {
                    path_to_view = path_to_view + '/' + $scope.latest + "--" + $rootScope.newlimit;
                }
            } else {
                //loading old comments
                if ($scope.oldest != '') {
                    path_to_view = path_to_view + '/' + $rootScope.oldlimit + "--" + $scope.oldest;
                }
            }

            console.log(path_to_view);
            views_datasource_get_view_result(path_to_view, {
                success: function (data) {
                    var length = data.comments.length;
                    if (length != 0) {
                        console.log(data);
                        for (var i = 0; i < length; i++) {
                            var cid = data.comments[i].comment.cid;
                            $scope['comment'][cid] = {};
                            $scope['comment'][cid].cid = cid;
                            $scope['comment'][cid].nid = data.comments[i].comment.nid;
                            $scope['comment'][cid].body = data.comments[i].comment.answer;
                            $scope['comment'][cid].time = data.comments[i].comment.time;
                            if (data.comments[i].comment.author_pic) {
                                $scope['comment'][cid].author_pic = data.comments[i].comment.author_pic;
                            }
                            $scope['comment'][cid].author = data.comments[i].comment.author;
                        }
                        console.log($scope['comment']);
                        if (!refresh) {
                            //如果上拉加载需要将时间戳下线设为本次数据最旧的时间
                            $scope.oldest = data.comments[length - 1].comment.cid;
                        }
                        if (refresh && data.comments[length - 1].comment.cid > $scope.latest) {
                            //如果下拉刷新且最旧的数据的时间戳都比目前上限要新
                            $scope.oldest = data.comments[length - 1].comment.cid;
                            $scope.listEnd = false;
                        }
                        if (refresh || $scope.latest == '') {
                            //如果下拉刷新需要重置最新时间
                            $scope.latest = data.comments[0].comment.cid;
                        }
                    }
                    if (!refresh && length < 10) {
                        $scope.listEnd = true;
                    }
                    $scope.$apply();
                    if (refresh) {
                        $scope.$broadcast('scroll.refreshComplete');
                    } else {
                        $scope.$broadcast('scroll.infiniteScrollComplete');
                    }
                }
            });
        };
    })

    .controller('ConsultAddCtrl', function ($scope,$ionicSideMenuDelegate,$ionicLoading,$state, $rootScope) {

        if($rootScope.user.uid == 0){
            alert("请登录后再进行操作");
            $state.go('tabs.my-account');
            return;
        }

        $scope.consult = {};

        $scope.submitForm = function () {

            $ionicLoading.show({
                content: '重新连接',
                animation: 'fade-in',
                showBackdrop: true,
                maxWidth: 200,
                showDelay: 0
            });
            var consultNode = {
                type:'consult',
                title:$scope.consult.title
                //body: {
                //    und: [
                //        {value: $scope.consult.body}
                //    ]
                //}
            };

            node_create(consultNode, {
                success: function (result) {
                    $ionicLoading.hide();
                    $state.go("tabs.consult-list");
                },
                error:function(){
                    $ionicLoading.hide();
                    $state.go("error");
                }
            });
        };
    });


